package com.kczyjg.hybird

import android.annotation.TargetApi
import android.content.Context
import android.net.http.SslError
import android.util.AttributeSet
import android.webkit.SslErrorHandler
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient

class H5WebView : WebView {

    // 构造函数1: 代码中new创建时使用
    constructor(context: Context) : super(context) {
        init()
    }

    // 构造函数2: XML布局中使用时调用
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    // 构造函数3: XML布局中带有style属性时调用
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    // 构造函数4: 兼容更高版本的style（API 21+）
    @TargetApi(21)
    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        init()
    }

    /**
     * 预加载WebView必须先初始化的内容
     */
    private fun init() {
        initJsApi()
        initSettings()
    }

    fun initJsApi() {
        addJavascriptInterface(NativeApi(this), "NativeApi")
    }

    fun initSettings() {
        setWebContentsDebuggingEnabled(true)
        settings.allowFileAccess = true
        settings.allowContentAccess = true
        //自适应屏幕
        settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        settings.loadWithOverviewMode = true
        //支持App内部javascript交互
        settings.javaScriptEnabled = true
        //设置可以支持缩放
        settings.setSupportZoom(false)
        //扩大比例的缩放
        settings.useWideViewPort = false
        //设置是否出现缩放工具
        settings.builtInZoomControls = false
        //设置缓存加载模式
        if (BuildConfig.DEBUG) {
            settings.cacheMode = WebSettings.LOAD_NO_CACHE
        } else {
            settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        }
        //编码方式
        settings.defaultTextEncodingName = "utf-8"
        //使用Dom存储
        settings.domStorageEnabled = true

        settings.allowFileAccess = true;
        settings.allowFileAccessFromFileURLs = true;
        settings.allowUniversalAccessFromFileURLs = true;
        settings.databaseEnabled = true;

        setWebChromeClient(object : WebChromeClient() {

        })
        setWebViewClient(object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)

            }

            override fun shouldOverrideUrlLoading(
                view: WebView,
                request: WebResourceRequest
            ): Boolean {
                return false
            }

            override fun onReceivedSslError(
                view: WebView,
                handler: SslErrorHandler,
                error: SslError
            ) {
                handler.proceed()
            }

        })
    }
}